package com.ruoyi.test;


import cn.hutool.core.util.ObjectUtil;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;

public class SubstringFinder {

    public static Set<String> findAllSubstrings(String input1,String input2,int limit) {
        //只保留数字、字母、中文
        String s1 = input1.replaceAll("[^0-9a-zA-Z\\u4e00-\\u9fa5]", "");
        String s2 = input2.replaceAll("[^0-9a-zA-Z\\u4e00-\\u9fa5]", "");

        Set<String> strings = new HashSet<>();
        List<String> stringList1 = getsubString(s1,limit);
        List<String> stringList2 = getsubString(s2,limit);
        for (int i = 0; i < stringList1.size(); i++) {
            for (int j = 0; j < stringList2.size(); j++) {
                if (stringList1.get(i).equals(stringList2.get(j))){
                    strings.add(stringList1.get(i));
                }
            }
        }
        List<String> collect = new ArrayList<>(strings);
        for (int i = 0; i < collect.size(); i++) {
            for (String s : collect) {
                if (collect.get(i).length() > s.length() &&
                    (!collect.get(i).equals(s)) &&
                    collect.get(i).contains(s)) {
                    strings.remove(s);
                }
            }
        }
        return strings;
    }
    private static List<String> getsubString(String inputString, int limit) {
        Set<String> substrings = new HashSet<>();

        for (int i = 0; i < inputString.length(); i++) {
            StringBuilder tempString = new StringBuilder();
            for (int j = i; j < inputString.length(); j++) {
                tempString.append(inputString.charAt(j));
                substrings.add(tempString.toString());
            }
        }

        List<String> collect = substrings.stream().filter(s -> s.length() >= limit).collect(Collectors.toList());

        return collect;
    }
    public static double calculateJaccardSimilarity(String strA, String strB) {
        // 将字符串转换为字符集合
//        System.out.println("原字符串："+strA);
        Set<Character> setA = new HashSet<>();
        for (char c : strA.toCharArray()) {
            setA.add(c);
//            System.out.println("拆分的字符串：" + c);
        }

        Set<Character> setB = new HashSet<>();
        for (char c : strB.toCharArray()) {
            setB.add(c);
        }

        // 计算交集和并集的大小
        Set<Character> intersection = new HashSet<>(setA);
        intersection.retainAll(setB);

        Set<Character> union = new HashSet<>(setA);
        union.addAll(setB);

        // 计算Jaccard相似度
        double result = (double) intersection.size() / union.size();

        // 格式化返回值为6位小数点
        DecimalFormat decimalFormat = new DecimalFormat("#.######");
        String formattedResult = decimalFormat.format(result);
        return Double.parseDouble(formattedResult);
    }
    /**
     * 测试方法
     * @param args
     */
    public static void main(String[] args) throws InterruptedException {
        String value = null;
        String value1 = null;
        try {
            // 加载PDF文件
            PDDocument document = PDDocument.load(new File("D:\\dev\\RuoYi-Vue-Plus\\ruoyi-admin\\src\\main\\resources\\山东奥辰建设工程集团有限公司投标文件-.pdf"));
            PDDocument document1 = PDDocument.load(new File("D:\\dev\\RuoYi-Vue-Plus\\ruoyi-admin\\src\\main\\resources\\qqqq.pdf"));
            // 获取PDF文件的内容
            String content = new PDFTextStripper().getText(document);
            if (ObjectUtil.isEmpty(content)){
                throw new EmptyStackException();
            }
            // 获取PDF文件的内容
            String content1 = new PDFTextStripper().getText(document1);
            if (ObjectUtil.isEmpty(content)){
                throw new EmptyStackException();
            }
            //处理字符串只要汉字数字字母
            value = content.replaceAll("[^0-9a-zA-Z\\u4e00-\\u9fa5]", "");
            value1 = content1.replaceAll("[^0-9a-zA-Z\\u4e00-\\u9fa5]", "");
            // 关闭文档
            document.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        assert value != null;
        //List<String> list = exactAllCommonSeq(value, value1);
        Set<String> strings = findAllSubstrings(value, value1, 15);
        System.out.println(strings);
        //System.out.println(list);
    }
//    /**
//     * 测试方法
//     * @param args
//     */
//    public static void main(String[] args) {
//        String inputA = "    /**\n" +
//            "     * wo 定义了两个文件路径 file1Pat@#$%^&h和file2Path，无人并使用FileReader和BufferedReader来读取文件内容。\n" +
//            "     * 然而然后，不利用SaaaaaatringBuilder将读取到的@#$%^&aaaaaaaaaaaaaaaaaaaaaaaa符转化为字符串并存储在file1Content和file2Content中。\n" +
//            "     * 判断ss有没有连续十个字符重复，s@#$%^&记重复的语句为红色\n" +
//            "     */";
//        String inputB = "    /**\n" +
//            "     * ta 定义了两个文@#$%^&件路径 file1Path和file2Path，有人aaaaaaaaaaaaaa并使用FilesdReader和BufferedReader来读取文件内容。\n" +
//            "     * 他说然后，就利用StringBuilder将读取到sdas的字符@#$%^&转化为字符串并存@#$%^&储在file1Content和file2Content中。\n" +
//            "     * 判断有没有连续十个字符重复，然后标记重复的@#$%^&语句为红色\n" +
//            "     */";
//
//        //只保留数字、字母、中文
//        String ss1 = inputA.replaceAll("[^0-9a-zA-Z\\u4e00-\\u9fa5]", "");
//        String[] strings1 = ss1.split(" ");
//        List<String> list1 = Arrays.stream(strings1).filter(s -> !s.equals("")).collect(Collectors.toList());
//
//        String ss2 = inputB.replaceAll("[^0-9a-zA-Z\\u4e00-\\u9fa5]", "");
//        String[] strings2 = ss2.split(" ");
//
//        double v = calculateJaccardSimilarity(ss1, ss2);
//        System.out.println(v);
//
//        List<String> list2 = Arrays.stream(strings2).filter(s -> !s.equals("")).collect(Collectors.toList());
//
//        System.out.println(findAllSubstrings(inputA,inputB,8));
//    }
}
